package com.hzlx.badGoods.service.impl;

import java.util.Date;
import java.util.List;

import com.hzlx.*;
import com.hzlx.badGoods.feign.BaseDataService;
import com.hzlx.badGoods.feign.MaterialService;
import com.hzlx.reports.domain.NonConformingReports;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hzlx.badGoods.mapper.MesBadGoodsMapper;
import com.hzlx.badGoods.domain.MesBadGoods;
import com.hzlx.badGoods.service.IMesBadGoodsService;
import com.hzlx.badGoods.feign.productionService;
/**
 * 待处理残次品Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-07
 */
@Service
public class MesBadGoodsServiceImpl implements IMesBadGoodsService

{
    @Resource
    private BaseDataService baseDataService;
    @Resource
    private MaterialService materialService;
    @Resource
    private productionService productionService;
    @Autowired
    private MesBadGoodsMapper mesBadGoodsMapper;

    /**
     * 查询待处理残次品
     * 
     * @param id 待处理残次品主键
     * @return 待处理残次品
     */
    @Override
    public MesBadGoods selectMesBadGoodsById(Long id)
    {
        return mesBadGoodsMapper.selectMesBadGoodsById(id);
    }

    /**
     * 查询待处理残次品列表
     * 
     * @param mesBadGoods 待处理残次品
     * @return 待处理残次品
     */
    @Override
    public List<MesBadGoods> selectMesBadGoodsList(MesBadGoods mesBadGoods)
    {
        return mesBadGoodsMapper.selectMesBadGoodsList(mesBadGoods);
    }

    /**
     * 新增待处理残次品
     * 
     * @param mesBadGoods 待处理残次品
     * @return 结果
     */
    @Override
    public int insertMesBadGoods(MesBadGoods mesBadGoods)
    {
        MesBadGoodsDto mesBadGoodsDto=new MesBadGoodsDto();
        mesBadGoodsDto.setProductId(mesBadGoods.getProductId());
        productionService.deleteByProductIdByDo(mesBadGoodsDto);

        return mesBadGoodsMapper.insertMesBadGoods(mesBadGoods);
    }

    /**
     * 修改待处理残次品
     * 
     * @param mesBadGoods 待处理残次品
     * @return 结果
     */
    @Override
    public int updateMesBadGoods(MesBadGoods mesBadGoods)
    {
        return mesBadGoodsMapper.updateMesBadGoods(mesBadGoods);
    }

    /**
     * 批量删除待处理残次品
     * 
     * @param ids 需要删除的待处理残次品主键
     * @return 结果
     */
    @Override
    public int deleteMesBadGoodsByIds(Long[] ids)
    {
        return mesBadGoodsMapper.deleteMesBadGoodsByIds(ids);
    }

    /**
     * 删除待处理残次品信息
     * 
     * @param id 待处理残次品主键
     * @return 结果
     */
    @Override
    public int deleteMesBadGoodsById(Long id)
    {
        return mesBadGoodsMapper.deleteMesBadGoodsById(id);
    }

    @Override
    public ResultDto<String> pass(MesBadGoods mesBadGoods) {
        //1.判断该次品当前工序是否是最后一道工序,如果是最后一道工序，将该次品直接添加到库存中
        if (mesBadGoods.getOperationId() == 6){
            MesFinishedGoodsInventoryDto mesFinishedGoodsInventoryDto = new MesFinishedGoodsInventoryDto();
            mesFinishedGoodsInventoryDto.setProductId(mesBadGoods.getProductId());
            mesFinishedGoodsInventoryDto.setProductFinishedId(Long.valueOf(0));
            materialService.addBadGoods(mesFinishedGoodsInventoryDto);
            System.out.println("添加到库存的产品信息为"+mesFinishedGoodsInventoryDto);
        }else {
            //2.如果不是最后一道，将参数中的对应的次品添加到对应的工序的下一道工序中
            ProductionProductsDto productionProductsDto = new ProductionProductsDto();
            //根据id查产品的信息
            ProductsDto productsDto = baseDataService.getResInfo(mesBadGoods.getProductId()).getData();
            System.out.println("查产品"+baseDataService.getResInfo(mesBadGoods.getProductId()));
            System.out.println("查产品Data"+baseDataService.getResInfo(mesBadGoods.getProductId()).getData());
            System.out.println("对应的产品为"+productsDto);
            //将对应的变量填进去
            productionProductsDto.setProductTypeId(Math.toIntExact(mesBadGoods.getProductId()));
            productionProductsDto.setProductName(productsDto.getProductName());
            productionProductsDto.setProductDescription(productsDto.getProductDescription());
            productionProductsDto.setUnitOfMeasure(productsDto.getUnitOfMeasure());
            productionProductsDto.setOperationId(Math.toIntExact(mesBadGoods.getOperationId()));
            productionProductsDto.setCreatedAt(new Date());
            productionProductsDto.setUpdatedAt(new Date());
            System.out.println("对应的加入生产产品的信息"+productionProductsDto);
            productionService.addBadGoods(productionProductsDto);
        }
        mesBadGoodsMapper.deleteMesBadGoodsById(mesBadGoods.getId());
        return ResultDto.<String>builder().code(200).msg("合格添加成功").build();
    }

    @Override
    public ResultDto<String> fail(MesBadGoods mesBadGoods) {
        //1.不合格就添加不合格品报告


//        nonConformingReports.setProductId(String.valueOf(mesBadGoods.getProductId()));
//        nonConformingReports.setUnitOfMeasure(productsDto.getUnitOfMeasure());
        //2.然后要把该次品返回到对应的工序重新制作---返工
        System.out.println(mesBadGoods);
        System.out.println(mesBadGoods.getProductId());
        ProductsDto productsDto = baseDataService.getResInfo(mesBadGoods.getProductId()).getData();
        System.out.println("对应的产品为"+productsDto);
        ProductionProductsDto productionProductsDto = new ProductionProductsDto();
        productionProductsDto.setProductTypeId(Math.toIntExact(mesBadGoods.getProductId()));
        productionProductsDto.setProductName(productsDto.getProductName());
        productionProductsDto.setProductDescription(productsDto.getProductDescription());
        productionProductsDto.setUnitOfMeasure(productsDto.getUnitOfMeasure());
        productionProductsDto.setOperationId(Math.toIntExact(mesBadGoods.getOperationId()-1));
        productionProductsDto.setCreatedAt(new Date());
        productionProductsDto.setUpdatedAt(new Date());
        System.out.println("对应的加入生产产品的信息"+productionProductsDto);
        productionService.addBadGoods(productionProductsDto);
        //3.删除
        mesBadGoodsMapper.deleteMesBadGoodsById(mesBadGoods.getId());
        return ResultDto.<String>builder().code(200).build();
    }
}
